﻿// //隔行换色
// var tr=document.getElementsByTagName("tr");
// for(var i=0;i<tr.length;i++){
//     if(i%2){
//         tr[i].style.background="rgb(146, 160,160)";
//     }else{
//         tr[i].style.background="white";
//     }
// }
// //删除当前行后更新换色
// function fnChange(){
// var tr=document.getElementsByTagName("tr");
//         for(var i=0;i<tr.length;i++){
//             if(i%2){
//                 tr[i].style.background="rgb(146, 160,160)";
//             }else{
//                 tr[i].style.background="white";
//             }
//         }
//         addAllprice();
// }

//删除当前行
function fnDeleteRow(_this){
    var tr=_this.parentNode.parentNode;
    var table=document.body.firstElementChild.firstElementChild;
    table.firstElementChild.removeChild(tr);
    addAllprice();
}

//全选
input.onclick=function (){
var checkRowNodes = document.getElementsByName("select");
 var selectall = document.getElementsByName("selectAll");
for(var i =0; i < selectall.length; i++){
   if(selectall[i].checked==false){
      for (var x = 0; x < checkRowNodes.length; x++) {
            checkRowNodes[x].checked = false;
        }
    }
     addAllprice();
}
addAllprice();
}

function checkAll(_this) {
//获取所有复选框
var checkRowNodes = document.getElementsByName("select");
var selectall = document.getElementsByName("selectAll");

for (var x = 0; x < checkRowNodes.length; x++) {
    checkRowNodes[x].checked = _this.checked
}
for (var i =0; i < selectall.length; i++) {
    selectall[i].checked = _this.checked;
}
addAllprice();
}

 //全选框未选中状态
function Fncheck(){
var checkRowNodes = document.getElementsByName("select");
var selectall = document.getElementsByName("selectAll");
for(var i =0; i < selectall.length; i++){
    if(selectall[i].checked==false){
        for (var x = 0; x < checkRowNodes.length; x++) {
            checkRowNodes[x].checked = false;
        }  
    }
addAllprice();
}


// 通过获取表格前复选框判断定义删除表格行函数
    function delRow() { 
    var table = document.getElementsByTagName("tbody")[0];
    if (confirm("确认删除所选？")) {
        //定义一个变量接收选中的复选框
        var checkboxs = document.getElementsByName("selectAll");
        //遍历整个table表
        for (var i = 1; i < checkboxs.length; i++) {
            if (checkboxs[i].checked==true) { //判断
                var n = checkboxs[i].parentNode.parentNode;
                //移除子元素
                table.removeChild(n);
                //改变循环变量
                i--;
            }
        }
    }
}
addAllprice();
}
//全部删除
function delRowAll() {
    var table = document.getElementsByTagName("tbody")[0];
    if (confirm("确认删除？")) {
        var checkboxs = document.getElementsByName("selectAll");
        for (var i = 1; i < checkboxs.length; i++) {    
                var n = checkboxs[i].parentNode.parentNode;
                table.removeChild(n);
                i--;
        }
    }
    addAllprice();
}

//+、-框
function FnAdd(_this){
    var borther= _this.parentNode.children[1];
    borther.value=parseInt(borther.value)+1;
    addAllprice();
}
function Fnjian(_this){
    var borther= _this.parentNode.children[1];
    
    if(parseInt(borther.value)>0){
        borther.value=parseInt(borther.value)-1;
    }
    addAllprice();
}

//计算总金额
window.onload = addAllprice();
 function addAllprice() {
    var tr = document.getElementsByTagName('tr');
    var sum = 0;
    for (i = 1; i < tr.length; i++) {
         if (tr[i].children[0].firstElementChild.checked){
             var price = tr[i].children[3].innerHTML;
             var num = tr[i].children[4].children[1].value;
            sum += price * num;
        }
     }
      var priceAll = document.getElementById('priceAll');
      priceAll.innerText = '￥'+sum.toFixed(2);
  }